var manager;
var imgManager;
var addManager;
var $modal = $('#ajax-modal');
var $Img = $("#menuimg");
$(function () {
    menuTree();
    $("#icons").find("i").click(function () {
        var icon = $(this).attr("class").replace("fa ", "");
        $Img.val(icon);
        $Img.next().attr("class", "fa " + icon);
        $("#stack2").modal('hide');
    });
});
/**
 * @title : 显示菜单树
 * */
function menuTree() {
    $.ajax({
        type: "post",
        url: _path + "/sysMenu/listSysMenu",
        success: function (data) {
            manager = $("#maingrid").ligerGrid({
                headerRowHeight: 36,
                rowHeight: 36,
                width: '99%',
                height: '97%',
                checkbox: false,
                alternatingRow: true,
                usePager: false,
                columns: [
                    { display: '选择', name: '', width: '4%', render: function (row) {
                        var html = '<input type="radio" name="raido" style="margin-top:5px" />';
                        return html;
                    }},
                    { display: 'ID', name: 'id', width: '0%', align: 'left', hide: true},
                    { display: '菜单名称', name: 'menuname', width: '17.5%', align: 'left', render: function (row) {
                        return "<i class='fa " + row.menuimg + "'></i>&nbsp;&nbsp;<a href='javascript:show()' id='show'>" + row.menuname + "</a>";
                    }},
                    { display: '顺序', name: 'sort', width: '5%', align: 'center', editor: { type: 'text'} },
                    { display: '上级菜单', name: 'pmenuName', width: '15%', align: 'center', editor: { type: 'text'} },
                    { display: '状态', name: 'isactive', width: '5%', type: 'int', align: 'center', render: function (row) {
                        if (row.isactive == "1") {
                            return "<i class='fa fa-check'></i>";
                        } else {
                            return "<i class='fa fa-frown-o'></i>";
                        }
                    }},
                    { display: 'URL', name: 'url', width: '30%', align: 'left', editor: { type: 'text'} }
                ], data: data, tree: { columnName: 'menuname' }, rownumbers: true, onSelectRow: onClickRow
            });
        }
    });
}
/**
 * 拓展行点击事件radio选中
 **/
function onClickRow(rowdata, rowindex, rowDomElement) {
    var $v = $(rowDomElement);
    $v.find(":radio").attr("checked", "checked")
}

/**
 * @title : 查看菜单详细信息
 */
function show(isAdd) {
    $("#menuid").val(isAdd ? '' : manager.getSelecteds()[0].id);
    $modal.on('shown.bs.modal', function () {
        var id = $("#menuid").val();
        if (id)
            App.ajax(_path + "/sysMenu/showSysMenu", "id=" + id, detail);
        else {
            detail({'id': '', 'menuname': '', 'pid': '', 'pmenuName': '', 'sort': '', 'menuimg': '', 'url': '', 'isactive': '1', 'remark': ''});
        }
    }).modal('show');
}

function detail(data) {
    $("#id").val(data.id);
    $("#menuname").val(data.menuname);
    $("#pid").val(data.pid);
    $("#pmenuName").val(data.pmenuName);
    $("#sort").val(data.sort);
    $Img.val(data.menuimg);
    $Img.next().attr("class", "fa " + data.menuimg);
    $("#url").val(data.url);
    $(":radio[value='" + data.isactive + "']").attr("checked", "checked");
    $("#remark").val(data.remark);
    treeSysMenu(data.pid);
}

/**
 * 删除机构
 * @return
 */
function del() {
    //获取选中记录行
    var rowid = manager.getSelecteds();
    var length = rowid.length;
    if (length != 1) {
        App.bubble(500, "请选择要删除的菜单！");
        return false;
    } else {
        if (manager.hasChildren(rowid[0])) {
            if ($.ligerDialog.confirm(rowid[0].menuname + "有子菜单是否删除？",
                function (yes) {
                    if (yes) {
                        delSysMenus(rowid[0].id);
                    }
                }));
        } else {
        	layer.confirm('您确定要删除吗？', function(){
        		delSysMenus(rowid[0].id);
    		}, function(){
    		
    		});
        }
    }
}
function delSysMenus(sysMenuIds) {
    $.ajax({
        type: "post",
        url: _path + "/sysMenu/delSysMenu?sysMenuIds=" + sysMenuIds,
        success: function (data) {
            //如果删除成功，则刷新页面！
            if (data["msg"] == "sucess") {
                App.bubble(200, "删除成功！");
            } else if (data["msg"] == "error") {
                App.bubble(500, "删除失败！");
            } else {
                App.bubble(500, data["msg"]);
            }
            //启用、停用后列表刷新
            // manager.set({url: _path + "/sysMenu/listSysMenu"});
            window.setTimeout(function(){window.location.reload()}, 500);
        },
        error: function () {
            App.bubble(500, "删除失败！");
        }
    });
}

//------详细信息修改-----
/**
 * @title : 以树的形式展示系统一级菜单
 * */
function treeSysMenu(nodeId) {
    $.ajax({
        type: "post",
        url: _path + "/sysMenu/getSysMenu",
        async: true,
        success: function (data) {
            var $$ = $("#menuTree");
            var treeData = [];
            treeData.push({
                'id': '-1',
                'icon': '',
                'text': '菜单列表',
                'isexpand': true,
                'pid': ""
            });
            var sysMenuList = data.sysMenu;
            for (var i = 0; i < sysMenuList.length; i++) {
                treeData.push({
                    'id': sysMenuList[i].id,
                    'icon': '',
                    'text': sysMenuList[i].menuname,
                    'isexpand': false,
                    'pid': "-1"
                });
            }
            $$.html("<div></div>");
            var treeManger = $$.children().ligerTree({
                data: treeData,
                idFieldName: 'id',
                parentIDFieldName: 'pid',
                checkbox: false,
                slide: true,
                //nodeWidth : 350,
                onClick: onclick
            });
            if (nodeId == '999999')
                nodeId = '-1';
            var $$ = $("#" + nodeId);
            if ($$.length > 0)
                $$.children().addClass("l-selected");
        }
    });
}
function onclick(note) {
    onclickPass(note.data);
}
function onclickPass(data) {
    var menuId = data.id;
    var pmenuName = data.text;
    if (menuId == "-1") {
        $("#pid").val("999999");
        $("#pmenuName").val("菜单列表");
    } else {
        $("#pid").val(menuId);
        $("#pmenuName").val(pmenuName);
    }
}
function save() {
    App.ajax(_path + "/sysMenu/saveSysMenu", $("#formDetail").serialize(), function (data) {
        if (data == '200') {
            $modal.modal('hide');
            sessionStorage.removeItem("menuHtml");
            window.location.reload();
        } else
            App.bubble(500, "保存失败！");
    });
}

function oper(type) {
    //获取选中记录行
    var rowid = manager.getSelecteds();
    var length = rowid.length;
    if (length != 1) {
        App.bubble(500, "请选择一个菜单！");
        return false;
    } else {
        App.ajax(_path + "/sysMenu/updBatch", "id=" + rowid[0].id + "&type=" + type, function (data) {
            //如果删除成功，则刷新页面！
            if (data.statusCode === 200) {
                App.bubble(200, "操作成功！");
                sessionStorage.removeItem("menuHtml");
            } else {
                App.bubble(500, "操作失败！");
            }
            //启用、停用后列表刷新
            // manager.set({url:_path+"/sysMenu/listSysMenu"});
            window.setTimeout(function(){window.location.reload()}, 500);
        });
    }
}